﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Word = Microsoft.Office.Interop.Word;

namespace WordTemplate2.Pannel
{
    public partial class CheckRefrence : Form
    {
        public int select_index = 0;
        public CheckRefrence()
        {
            InitializeComponent();

            GetRefrenceList();
        }

        private void GetRefrenceList()
        {
            object move = Word.WdMovementType.wdMove;
            object extend = Word.WdMovementType.wdExtend;
            object line = Word.WdUnits.wdLine;
            object character = Word.WdUnits.wdCharacter;

            object number = 1;
            List<string> list = new List<string>();
            Globals.ThisDocument.MoveBookmarkStart("mbookmark4_ref");
            Word.Range r = Globals.ThisDocument.GetBookmarkRange("mbookmark4_ref");
            Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref move);
            do
            {
                Globals.ThisDocument.Application.Selection.HomeKey(ref line, ref move);
                Globals.ThisDocument.Application.Selection.EndKey(ref line, ref extend);
                string str = Globals.ThisDocument.Application.Selection.Text;
                int space_count = 0;
                int count = 0;
                bool has_char = false;
                foreach(char a in str)
                {
                    if (a.ToString() != " ")
                    {
                        has_char = true;
                    }
                    else if(a.ToString() == " " && has_char)
                    {
                        if (space_count == 1)
                        {
                            string str1 = str.Substring(0, count);
                            bool skip = false;
                            foreach (string s in list)
                            {
                                if(s == str1)
                                {
                                    skip = true;
                                    break;
                                }
                            }
                            if (!skip)
                            {
                                if (str.Contains("-"))
                                {
                                    int line_count = 0;
                                    foreach (char a1 in str)
                                    {
                                        
                                        if (a1.ToString() == "-")
                                        {
                                            line_count = line_count == 0 ? 1 : line_count;
                                            string str2 = str1.Substring(0, line_count);

                                            bool is_exist_in_list = false;
                                            foreach (string str_ref in list)
                                            {
                                                if (str_ref == str2)
                                                {
                                                    is_exist_in_list = true;
                                                    break;
                                                }
                                            }

                                            if(!is_exist_in_list)
                                            {
                                                list.Add(str2);
                                            }
                                            break;
                                        }
                                        line_count++;

                                    }
                                }
                                else
                                {
                                    bool is_exist_in_list = false;
                                    foreach (string str_ref in list)
                                    {
                                        if (str_ref == str1)
                                        {
                                            is_exist_in_list = true;
                                            break;
                                        }
                                    }

                                    if (!is_exist_in_list)
                                    {
                                        list.Add(str1);
                                    }
                                    break;
                                   
                                }
                            } 
                            break;
                        }
                        space_count++;
                    }
                    count++;
                }
                Globals.ThisDocument.Application.Selection.MoveDown(ref line, ref number, ref move);

            }
            while (Globals.ThisDocument.Application.Selection.Range.End <= r.End);

            foreach (string a in list)
            {
                checkedListBox1.Items.Add(a,true);
            }
        

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkedListBox1.Items.Count != 0 && checkedListBox1.Items.Count > select_index && select_index >= 0)
            {
                checkedListBox1.Items[select_index] = textBox1.Text;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                int id = checkedListBox1.Items.Add(textBox2.Text, true);
                checkedListBox1.SelectedIndex = id;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bool flag = Globals.ThisDocument.CheckBookmarkExist("mbookmark4_ref");
            if (!flag)
            {
                MessageBox.Show("没有找到引用文件！");
                return;
            }

            if (checkedListBox1.Items.Count == 0)
            {
                MessageBox.Show("检查过程中没有发现问题");
                return;
            }

            List<string> list = new List<string>();
            List<string> list_new = new List<string>();


            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                {
                    string str = (string)checkedListBox1.Items[i];
                    list.Add(str);
                }
            }

            object wholestory = Word.WdUnits.wdStory;
            object extend = Word.WdMovementType.wdExtend;

            Globals.ThisDocument.MoveBookmarkEnd("mbookmark4_ref");
            Globals.ThisDocument.Application.Selection.EndKey(ref wholestory, ref extend);
            Word.Range r1 = Globals.ThisDocument.Application.Selection.Range;

            foreach (string str in list)
            {
                if (!r1.Text.Contains(str))
                {
                    list_new.Add(str);
                }
            }

            if (list_new.Count > 0)
            {
                string str_miss = "";
                foreach (string str in list_new)
                {
                    if (str_miss != "")
                    {
                        str_miss += ",";
                    }
                    str_miss += str;
                }
                MessageBox.Show(str_miss + "没有找到!");
            }
            else
            {
                MessageBox.Show("检查过程中没有发现问题");
            }

 
            Globals.ThisDocument.MoveBookmarkEnd("mbookmark4_ref");
            
            this.Visible = false;
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            this.Close();
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            select_index = checkedListBox1.SelectedIndex;
        }
    }
}
